package com.itheima.controller.user;

import com.itheima.common.constant.RedisMessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.dto.SubmitOrderDTO;
import com.itheima.service.UserOrderService;
import com.itheima.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * 体检预约接口
 */
@RestController("userOrderController")
@RequestMapping("/order")
@Slf4j
public class OrderController {
  @Autowired
  private UserOrderService userOrderService;
  @Autowired
  private RedisTemplate redisTemplate;

  /**
   * 体检预约
   * @param submitOrderDTO
   * @return
   */
  @PostMapping("submitOrder")
    public Result submitOrder(@RequestBody SubmitOrderDTO submitOrderDTO) {
    String telephone = submitOrderDTO.getTelephone();
    //取得redis验证码
    String redisValidateCode = (String) redisTemplate.opsForValue().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
    //与输入进来的验证码进行对比(equal方法记住将常量.equal)
    //先判定redis库中验证码存在
    if (redisValidateCode != null) {
      if (redisValidateCode.equals(submitOrderDTO.getValidateCode())) {
        userOrderService.submitOrder(submitOrderDTO);
      } else {
        throw  new RuntimeException("请输入正确的验证码");
      }
    }else {
      throw new RuntimeException("验证码以失效");
    }
    return Result.success("预约成功");
  }
  /**
   * 根据预约id查询预约信息
   */
  @PostMapping("/findById")
  public Result<OrderVO> findById(@RequestParam String id) {
    log.info("查询预约信息的id：{}",id);
    OrderVO orderVO = userOrderService.findById(id);
    return  Result.success(orderVO,"查询预约信息成功");
  }
}
